package com.sskj.mine.verify;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.hey.lib.HeySpinner;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/01
 */

public class VerifySecondActivity extends BaseActivity<VerifySecondPresenter> {

    @BindView(R2.id.image_1)
    ImageView image1;
    @BindView(R2.id.image_2)
    ImageView image2;
    @BindView(R2.id.image_3)
    ImageView image3;
    @BindView(R2.id.image_4)
    ImageView image4;
    @BindView(R2.id.image_5)
    ImageView image5;
    @BindView(R2.id.image_6)
    ImageView image6;
    @BindView(R2.id.image_7)
    ImageView image7;
    @BindView(R2.id.idcard_layout)
    LinearLayout idcard_layout;
    @BindView(R2.id.passport_layout)
    LinearLayout passport_layout;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    @BindView(R2.id.verify_type)
    HeySpinner verifyType;

    private final int SELECT_HAND = 1000;
    private final int SELECT_FRONT = 1001;
    private final int SELECT_BACK = 1002;
    private final int SELECT_PS_FORNT = 1003;
    private final int SELECT_PS_HAND = 1004;
    private final int SELECT_CZ_SFZ = 1005;
    private final int SELECT_CZ_PS = 1006;

    private String path1, path2, path3;
    private int position = 1;
    private String imgFirst;
    private String imgSecond;
    private String imgThrid;
    private String imgFourth;
    private String imgPath1;
    private String imgPath2;
    private String imgPath3;
    private int type = 1;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_verify_second;
    }

    @Override
    public VerifySecondPresenter getPresenter() {
        return new VerifySecondPresenter();
    }

    @Override
    public void initView() {
        setTitle(App.INSTANCE.getString(R.string.mine_verifySecondActivity1_j));
    }

    @Override
    public void initData() {
        List<String> types = new ArrayList<>();
        types.add(App.INSTANCE.getString(R.string.mine_verifySecondActivity2_j));
        types.add(App.INSTANCE.getString(R.string.mine_verifySecondActivity3_j));
        verifyType.attachData(types);
        verifyType.setOnSelectListener(position -> {
            type = position + 1;
            changeLayout();
        });
    }

    private void changeLayout() {
        if (type == 1) {
            idcard_layout.setVisibility(View.VISIBLE);
            passport_layout.setVisibility(View.GONE);
        } else {
            passport_layout.setVisibility(View.VISIBLE);
            idcard_layout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initEvent() {

        ClickUtil.click(image1, () -> selectImage(SELECT_HAND));
        ClickUtil.click(image2, () -> selectImage(SELECT_FRONT));
        ClickUtil.click(image3, () -> selectImage(SELECT_BACK));
        ClickUtil.click(image4, () -> selectImage(SELECT_PS_FORNT));
        ClickUtil.click(image5, () -> selectImage(SELECT_PS_HAND));
        ClickUtil.click(image6, () -> selectImage(SELECT_CZ_SFZ));
        ClickUtil.click(image7, () -> selectImage(SELECT_CZ_PS));
        ClickUtil.click(submitBtn, () -> {
            if (type == 1) {
                if (TextUtils.isEmpty(imgFirst)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifySecondActivity4_j));
                    return;
                }
                if (TextUtils.isEmpty(imgSecond)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifySecondActivity5_j));
                    return;
                }
                if (TextUtils.isEmpty(imgThrid)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifySecondActivity6_j));
                    return;
                }
                if (TextUtils.isEmpty(imgFourth)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifySecondActivity7_j));
                    return;
                }
                mPresenter.verifySecond(imgFirst, imgSecond, imgThrid,imgFourth);
            } else {
                if (TextUtils.isEmpty(imgPath1)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifySecondActivity8_j));
                    return;
                }
                if (TextUtils.isEmpty(imgPath2)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifySecondActivity9_j));
                    return;
                }
                if (TextUtils.isEmpty(imgPath3)) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_verifySecondActivity7_j));
                    return;
                }
                mPresenter.verifySecond(imgPath1,"", imgPath2, imgPath3);
            }
        });

    }

    @SuppressLint("CheckResult")
    private void selectImage(int requestCode) {
        new RxPermissions(this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) {
                        PictureSelector.create(this)
                                .openGallery(PictureMimeType.ofImage())
                                .previewImage(true)
                                .isCamera(true)
                                .imageFormat(PictureMimeType.JPEG)
                                .maxSelectNum(1)
                                .compress(true)
                                .forResult(requestCode);
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            if (selectList != null && selectList.size() > 0) {
                LocalMedia media = selectList.get(0);
                String url;
                if (media.isCompressed()) {
                    url = media.getCompressPath();
                } else {
                    url = media.getPath();
                }
                switch (requestCode) {
                    case SELECT_HAND:
                        position = 3;
                        Glide.with(this).load(url).into(image1);
                        break;
                    case SELECT_FRONT:
                        position = 1;
                        Glide.with(this).load(url).into(image2);
                        break;
                    case SELECT_BACK:
                        position = 2;
                        Glide.with(this).load(url).into(image3);
                        break;
                    case SELECT_PS_FORNT:
                        position = 4;
                        Glide.with(this).load(url).into(image4);
                        break;
                    case SELECT_PS_HAND:
                        position = 5;
                        Glide.with(this).load(url).into(image5);
                        break;
                    case SELECT_CZ_SFZ:
                        position = 6;
                        Glide.with(this).load(url).into(image6);
                        break;
                    case SELECT_CZ_PS:
                        position = 7;
                        Glide.with(this).load(url).into(image7);
                        break;
                }
                mPresenter.submitVerify(new File(url), position);
            }
        }

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, VerifySecondActivity.class);
        context.startActivity(intent);
    }

    public void verifySecondSuccess() {
        finish();
    }

    public void putList(String url, int position) {
        switch (position) {
            case 1:
                imgFirst = url;
                break;
            case 2:
                imgSecond = url;
                break;
            case 3:
                imgThrid = url;
                break;
            case 4:
                imgPath1 = url;
                break;
            case 5:
                imgPath2 = url;
                break;
            case 6:
                imgFourth=url;
                break;
            case 7:
                imgPath3=url;
                break;
        }
    }

    public void uploadFail(int position) {
        switch (position) {
            case 1:
                Glide.with(this).load(getResources().getDrawable(R.mipmap.mine_id_card_front)).into(image1);
                break;
            case 2:
                Glide.with(this).load(getResources().getDrawable(R.mipmap.mine_ic_card_back)).into(image2);
                break;
            case 3:
                Glide.with(this).load(getResources().getDrawable(R.mipmap.mine_hand_ic_card)).into(image3);
                break;
        }
    }

}
